package com.ubing.boot.notesforjava.utils;

import java.io.*;
import java.net.URL;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;

class DownLoadThread extends Thread{
    int startIndex;
    int threadId;
    String path;
    byte[] bytes;
    RandomAccessFile fos;
    public DownLoadThread(int startIndex, int threadId,String path,byte[] bytes) {
        super();
        this.startIndex = startIndex;
        this.threadId = threadId;
        this.path = path;
        this.bytes = bytes;
    }

    @Override
    public void run(){
        try {
            fos=new RandomAccessFile(path,"rw");
            int total = 0;
//            FileChannel channel = fos.getChannel();
//            MappedByteBuffer map = channel.map(FileChannel.MapMode.READ_WRITE, 0, 4096); 堆外内存，
            fos.seek(startIndex);
            fos.write(bytes);
            total += bytes.length;
            System.out.println(Thread.currentThread().getName() + "下载了" + total);
            fos.close();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    };
} 